export const filterColumns = (list, columnsShow) => {
  console.warn("----- 字段模板 -----")
  console.log(list, columnsShow)
  let param = {
    columns: [...initColumnsKey(list)],
    key: "",
  }
  const filterList = (list, keys) => {
    let values = list.filter((item) => {
      if (item.children) {
        item.children = filterList(item.children, keys)
        return item.children.length
      } else {
        return keys.includes(item.fieldShow)
      }
    })
    return values
  }

  if (columnsShow.length) {
    console.log("----- 字段筛选 -----")
    param.columns = filterList(param.columns, columnsShow)
  }

  param.columns.forEach((item) => {
    if (item?.children) return
    param.key = item.fieldShow
  })
  console.warn("----- 字段模板 筛选后数据 -----", param)
  return param
}

export const initColumnsKey = (list) => {
  const initItem = (list) => {
    list.forEach((item) => {
      if (item.children) {
        initItem(item.children)
      } else {
        // 如 外部已初始化  则不处理
        item.fieldShow = item.fieldShow || item.field
      }
    })
  }
  initItem(list)
  return list
}
